package com.test.floatdemo;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mstar.android.tvapi.common.TvManager;
import com.mstar.android.tvapi.common.exception.TvCommonException;


public class FloatWindowBigView extends LinearLayout {
	/**
	 * 记录大悬浮窗的宽度
	 */
	public static int viewWidth;
	/**
	 * 记录大悬浮窗的高度
	 */
	public static int viewHeight;
	private Context context;
	private final static String TAG = "smart";
	private final static int MSG_INIT_IIS_SUCCESS = 1;
	private final static int MSG_INIT_IIS_FAIL = 2;
	public static String[] cmdArr = new String[]{
			"SetAmpRegister 50:00,00,00,00",
			"SetAmpRegister 29:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 2A:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 2B:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 2C:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 2D:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 2E:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 2F:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 30:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 31:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 32:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 33:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 34:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 35:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 36:00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00",
			"SetAmpRegister 3A:00,7F,FF,03,00,00,00,FB",
			"SetAmpRegister 3B:00,7F,FF,03,00,00,00,FB",
			"SetAmpRegister 3C:00,7F,FF,03,00,00,00,FB",
			"SetAmpRegister 40:FC,83,10,D4",
			"SetAmpRegister 41:0F,C0,00,00",
			"SetAmpRegister 42:00,08,42,10",
			"SetAmpRegister 46:00,00,00,00"

	};
	public FloatWindowBigView(final Context context) {
		super(context);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.float_window_big, this);
		View view = findViewById(R.id.big_window_layout);
		viewWidth = view.getLayoutParams().width;
		viewHeight = view.getLayoutParams().height;
		Button btnSetIISRegister = (Button)findViewById(R.id.SetIISRegister);
		Button Exit = (Button)findViewById(R.id.Exit);
		btnSetIISRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				new Thread(new InitIISThread()).start();
			}
		});
		Exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				MyWindowManager.removeBigWindow(context);
				MyWindowManager.createSmallWindow(context);
			}
		});
	}
	private class InitIISThread implements Runnable{
		@Override
		public void run() {
			try {
				boolean isSuccess = true;
				for(String str:cmdArr) {
					if(!TvManager.getInstance().setTvosInterfaceCommand(str)){
						isSuccess = false;
						break;
					}
					Log.d(TAG, str);
					Thread.sleep(100);
				}
				if(isSuccess){
					handler.sendEmptyMessage(MSG_INIT_IIS_SUCCESS);
				}else{
					handler.sendEmptyMessage(MSG_INIT_IIS_FAIL);
				}
			} catch (TvCommonException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	private Handler handler=new Handler(){
		public void handleMessage(Message msg){
			switch (msg.what) {
				case MSG_INIT_IIS_SUCCESS:
					Log.d(TAG, "INIT IIS SUCCESS");
					Toast.makeText(context,"INIT IIS SUCCESS",Toast.LENGTH_LONG).show();
					break;
				case MSG_INIT_IIS_FAIL:
					Log.d(TAG, "INIT IIS FAIL");
					Toast.makeText(context,"INIT IIS FAIL",Toast.LENGTH_LONG).show();
				default:
					break;
			}
		}
	};
}
